import os
from pathlib import Path

from config.setting import DATA_YAML_PATH
from lib.creat_case import create,remove_case,create_par_case
from lib.error_log import ErrorLog
from utils.get_setting_utils import OperationYaml

log = ErrorLog().get_log()


def batch_generation_test_cases(file_dir):
    """
    批量生成测试用例.py文件
    :param file_dir: 填写data下面的par或者single,par代表,single代表单接口的数据
    :return:
    """
    case_data_path = os.path.join(DATA_YAML_PATH,file_dir)
    if case_data_path and os.path.exists(case_data_path):
        p = Path(case_data_path)
        for file in p.rglob("*.yaml"):
            if file_dir == 'sigle':
                print(file)
                log.info(f"""创建{file.name}的测试用例""")
                create(file)
            if file_dir =='par':
                log.info(f"""创建{file.name}的参数化测试用例""")
                create_par_case(file)
                print('这是多接口',file)


def init():
    """初始化部分信息"""
    settings = OperationYaml.read_yaml()
    pytest_cmd = settings['cmd']['pytest_cmd']
    allure_cmd = settings['cmd']['allure_cmd']
    open_allure_cmd = settings['cmd']['open_allure_cmd']
    print(pytest_cmd)
    print(allure_cmd)
    print(open_allure_cmd)
    os.system(pytest_cmd)
    os.system(allure_cmd)

def main():
    """
    step1 读取yaml数据然后自动创建测试用例
    step2 执行测试用例
    step3 生成allure 报告
    :return:
    """
    batch_generation_test_cases('single')
    init()
    remove_case()

if __name__=='__main__':
    main()